package com.example.repair

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.common.view.view.BaseMvvmActivity
import com.example.image.glide.GlideImgLoader
import com.example.repair.databinding.ActivityRepairBinding
import com.example.repair.entity.RepairParam
import com.example.repair.vm.RepairViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/repair/RepairActivity")
class RepairActivity : BaseMvvmActivity<ActivityRepairBinding>() {
    override fun getLayoutId(): Int=R.layout.activity_repair
    val repairViewModel:RepairViewModel by viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.carTime2.text= SPUtils.getInstance().getString("gaode").toString()
        //点击跳转报修列表
        mDataBinding.gotoRepair.setOnClickListener {
            ARouter.getInstance().build("/repair/RepairListActivity").navigation()
        }
        mDataBinding.today.text=SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(System.currentTimeMillis())
        mDataBinding.iv1.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        mDataBinding.btnSubmit.setOnClickListener {
            var repairParam=RepairParam(
             contactMethod = "13955667788",
             faultDescription= mDataBinding.faultDescription.text.toString(),
             faultPhotos= "",
             id=0,
             orderNumber= "WX"+SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis()),
             processingStatus="待处理",
             repairLocation= "1",
             repairName= "哈基米",
             repairTime= mDataBinding.today.text.toString(),
             userId= 1
            )
            repairViewModel.postRepair(repairParam)
        }
        repairViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
        }
        repairViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode== RESULT_OK){
            val bean=data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideImgLoader.loadImage(this,bean.path,mDataBinding.iv1)
            Glide.with(this).load(R.drawable.ic_add_icon).into(mDataBinding.iv2)
        }
    }

}